﻿using System;
using System.Drawing;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace Client
{
    /// <summary>
    /// The main DirectX class - singleton pattern so there can only be one instance.
    /// </summary>
    public sealed class DirectText : IDisposable
    {
        public static readonly DirectText Me = new DirectText();

        public Microsoft.DirectX.Direct3D.Font DefGameFont { get; private set; }

        public void LoadFonts()
        {
            CreateDefFont();
        }


        public Microsoft.DirectX.Direct3D.Font CreateGameFont(FontDescription description)
        {
            //int nYsize;

            // ÆùÆ®Å©±â ¸ÂÃß±â.
            /*if (m_pddsBackBuffer)
            {
                HDC hDC;
                m_pddsBackBuffer->GetDC(&hDC);
                nYsize = -MulDiv(nHeight, GetDeviceCaps(hDC, LOGPIXELSY), 72);
                m_pddsBackBuffer->ReleaseDC(hDC);
            }*/

            return new Microsoft.DirectX.Direct3D.Font(DirectX.Me.DirectXDevice, description);
        }

        public void CreateDefFont()
        {
            // Create a default new font, make sure we free existing font
            if (DefGameFont != null)
            {
                DefGameFont.Dispose();
                DefGameFont = null;
            }

            FontDescription description = new FontDescription();
            description.CharSet = CharacterSet.Default;
            description.FaceName = "Arial";
            description.Height = 12;
            description.Width = 5;
            description.Weight = FontWeight.Normal;
            description.IsItalic = false;
            description.OutputPrecision = Precision.Default;
            description.Quality = FontQuality.Default;
            description.PitchAndFamily = PitchAndFamily.DefaultPitch | PitchAndFamily.FamilyRoman;

            DefGameFont = CreateGameFont(description);
        }

        #region Draw Texts
        public void DrawText(string str, int x, int y, Color c)
        {
            DefGameFont.DrawText(DirectX.Me.GameSprite, str, x, y, c);
        }

        public void DrawText(string str, Rectangle rc, Color c)
        {
            DefGameFont.DrawText(DirectX.Me.GameSprite, str, rc, DrawTextFormat.VerticalCenter | DrawTextFormat.Center | DrawTextFormat.SingleLine, c);
        }

        public void DrawShadowText(string text, int x, int y, Color bColor, Color fColor)
        {
            DefGameFont.DrawText(DirectX.Me.GameSprite, text, x + 1, y + 1, bColor);
            DefGameFont.DrawText(DirectX.Me.GameSprite, text, x, y, fColor);
        }

        public void DrawBoldText(string text, int x, int y, Color bColor, Color fColor)
        {
            DefGameFont.DrawText(DirectX.Me.GameSprite, text, x - 1, y, bColor);
            DefGameFont.DrawText(DirectX.Me.GameSprite, text, x + 1, y, bColor);
            DefGameFont.DrawText(DirectX.Me.GameSprite, text, x, y - 1, bColor);
            DefGameFont.DrawText(DirectX.Me.GameSprite, text, x, y + 1, bColor);

            DefGameFont.DrawText(DirectX.Me.GameSprite, text, x, y, fColor);
        }

        public Rectangle MeasureString(string text)
        {
            return DefGameFont.MeasureString(DirectX.Me.GameSprite, text,
                DrawTextFormat.Center | DrawTextFormat.SingleLine, Color.Black);
        }
        #endregion


        #region "IDisposable Support"
        private bool disposedValue;      // To detect redundant calls

        private void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    // TODO: dispose managed state (managed objects).
                    // Free other state (managed objects).
                    if (DefGameFont != null)
                    {
                        DefGameFont.Dispose();
                        DefGameFont = null;
                    }
                }

                // TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
                // TODO: set large fields to null.
                disposedValue = true;
            }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        #endregion
    }
}
